package com.small.nacos.naming.consistency.ephemeral.distro;

import com.small.nacos.core.cluster.ServerMemberManager;
import com.small.nacos.core.distributed.distro.component.DistroComponentHolder;
import com.small.nacos.core.distributed.distro.task.DistroTaskEngineHolder;
import com.small.nacos.naming.consistency.KeyBuilder;
import com.small.nacos.naming.consistency.ephemeral.distro.combined.DistroHttpCombinedKeyTaskFailedHandler;
import com.small.nacos.naming.consistency.ephemeral.distro.component.DistroDataStorageImpl;
import com.small.nacos.naming.consistency.ephemeral.distro.component.DistroHttpAgent;
import com.small.nacos.naming.core.DistroMapper;
import com.small.nacos.naming.misc.GlobalConfig;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 * @Author zhoujin
 * @Date 2022/11/13 18:41
 */
@Component
public class DistroHttpRegistry {
    private final DistroComponentHolder componentHolder;

    private final DistroTaskEngineHolder taskEngineHolder;

    private final DataStore dataStore;

    private final DistroMapper distroMapper;

    private final GlobalConfig globalConfig;

    private final DistroConsistencyServiceImpl consistencyService;

    private final ServerMemberManager memberManager;

    public DistroHttpRegistry(DistroComponentHolder componentHolder, DistroTaskEngineHolder taskEngineHolder,
                              DataStore dataStore, DistroMapper distroMapper, GlobalConfig globalConfig,
                              DistroConsistencyServiceImpl consistencyService, ServerMemberManager memberManager) {
        this.componentHolder = componentHolder;
        this.taskEngineHolder = taskEngineHolder;
        this.dataStore = dataStore;
        this.distroMapper = distroMapper;
        this.globalConfig = globalConfig;
        this.consistencyService = consistencyService;
        this.memberManager = memberManager;
    }

    @PostConstruct
    public void doRegister() {
        componentHolder.registerDataStorage(KeyBuilder.INSTANCE_LIST_KEY_PREFIX,
                new DistroDataStorageImpl(dataStore, distroMapper));
        componentHolder.registerTransportAgent(KeyBuilder.INSTANCE_LIST_KEY_PREFIX, new DistroHttpAgent(memberManager));
        componentHolder.registerFailedTaskHandler(KeyBuilder.INSTANCE_LIST_KEY_PREFIX,
                new DistroHttpCombinedKeyTaskFailedHandler(globalConfig, taskEngineHolder));
        componentHolder.registerDataProcessor(consistencyService);
    }
}
